export function calculateContextMenuPosition(event, menuDimensions = { width: 160, height: 150 }) {
  const clickX = event.clientX;
  const clickY = event.clientY;

  const windowWidth = window.innerWidth;
  const windowHeight = window.innerHeight;
  const menuBuffer = 5; // Buffer from window edge

  let menuX = clickX;
  let menuY = clickY;

  // Adjust if menu overflows on the right
  if (clickX + menuDimensions.width > windowWidth - menuBuffer) {
    menuX = windowWidth - menuDimensions.width - menuBuffer;
  }

  // Adjust if menu overflows on the bottom
  if (clickY + menuDimensions.height > windowHeight - menuBuffer) {
    menuY = windowHeight - menuDimensions.height - menuBuffer;
  }

  // Adjust if menu overflows on the left (less common for initial click, but good to have)
  if (menuX < menuBuffer) {
    menuX = menuBuffer;
  }

  // Adjust if menu overflows on the top (less common for initial click)
  if (menuY < menuBuffer) {
    menuY = menuBuffer;
  }
  
  // Determine if the menu was pushed from its ideal horizontal position relative to the click
  // This is useful for submenus that might need to know if the parent was shifted.
  const adjustedLeft = menuX < clickX; 

  return { 
    top: menuY, 
    left: menuX, 
    // adjustedLeft indicates if the menu had to be shifted left from the click point
    // or from the right edge of the screen.
    adjustedLeft: menuX < clickX || (clickX + menuDimensions.width > windowWidth - menuBuffer),
    // adjustedTop indicates if the menu had to be shifted up from the click point
    // or from the bottom edge of the screen.
    adjustedTop: menuY < clickY || (clickY + menuDimensions.height > windowHeight - menuBuffer)
  };
}

export function calculateSubmenuPosition(parentRect, submenuDimensions = { width: 100, height: 100 }, preferredGap = 2) {
  const windowWidth = window.innerWidth;
  const windowHeight = window.innerHeight;
  const menuBuffer = 5; // Buffer from window edge

  let submenuX, submenuY;
  let positionedRight = true; // Assume submenu is to the right initially

  // Ideal vertical position: align top of submenu with top of parent item
  submenuY = parentRect.top;

  // Ideal horizontal position: to the right of the parent
  submenuX = parentRect.right + preferredGap;

  // Check for right overflow
  if (submenuX + submenuDimensions.width > windowWidth - menuBuffer) {
    // Try to position to the left of the parent
    submenuX = parentRect.left - submenuDimensions.width - preferredGap;
    positionedRight = false;
  }

  // Check for left overflow (if it was moved to the left)
  if (!positionedRight && submenuX < menuBuffer) {
    // If still overflows left, try to push it back to the right,
    // even if it means overlapping the parent slightly or going off a bit.
    // Or, as a simpler fallback, position it at the window edge.
    // For now, let's just stick it to the edge.
    submenuX = menuBuffer; 
    // In a more complex scenario, you might try to fit it on the right again
    // and accept some parent overlap if window is too narrow.
  } else if (positionedRight && submenuX < menuBuffer) {
    // This case should ideally not happen if parentRect.right was already on screen.
    // But as a safeguard:
    submenuX = menuBuffer;
  }


  // Check for bottom overflow
  if (submenuY + submenuDimensions.height > windowHeight - menuBuffer) {
    submenuY = windowHeight - submenuDimensions.height - menuBuffer;
  }

  // Check for top overflow
  if (submenuY < menuBuffer) {
    submenuY = menuBuffer;
  }

  return {
    top: submenuY,
    left: submenuX,
    positionedRight // Indicates if the submenu is to the right or left of the parent
  };
} 